package nba2kmodtool.filedesc.util3d;

/**
 * @author JemiZhuu
 * @category DX3Dmath
 * @version 1.0
 * @since 2010-10-19
 */
public class DX3Dmath {

	public static float[] nativeD3DXFloat16To32Array(short[] values) {
		return D3DXLibNative.D3DXFloat16To32Array(values);
	}

	public static short[] nativeD3DXFloat32To16Array(float[] values) {
		return D3DXLibNative.D3DXFloat32To16Array(values);
	}

	// Converts an array 16-bit floats to 32-bit floats
	public static float[] D3DXFloat16To32Array(short[] values) {
		float[] results = new float[values.length];
		for (int i = 0; i < results.length; i++) {
			int tv = values[i];
			int sign = tv >>> 15;
			int exponent = (tv >>> 11) & 0xf;
			int mantissa = tv & 0x7ff;
			int ret = sign << 31;
			if (sign == 0) {
				ret |= ((exponent | 0x7) << 27);
			} else {
				ret |= ((exponent & 0xf) << 27);
			}
			ret |= ((exponent & 0x7) << 24);
			ret |= (mantissa << 13);
			results[i] = Float.intBitsToFloat(ret);
		}
		return results;
	}

	// Converts an array 32-bit floats to 16-bit floats
	public static short[] D3DXFloat32To16Array(float[] values) {
		short[] results = new short[values.length];
		for (int i = 0; i < results.length; i++) {
			int tv = Float.floatToIntBits(values[i]);
			int sign = (tv >>> 31) & 0x1;
			int exponent = 0;
			if (sign == 0) {
				exponent = (tv >> 27) | 0x7;
			} else {
				exponent = (tv >> 27) & 0xf;
			}
			int mantissa = (tv >> 13) & 0x7ff;
			int ret = sign << 15;
			ret |= (exponent << 11);
			ret |= mantissa;
			results[i] = (short) ret;
		}
		return results;
	}
}
